/**
 *
 */
package com.yangtao.prototype.system.user.service.impl;

import com.yangtao.framework.service.BaseService;
import com.yangtao.framework.util.StringHelper;
import com.yangtao.prototype.system.dao.OrganizationDao;
import com.yangtao.prototype.system.dao.RoleDao;
import com.yangtao.prototype.system.dao.UserDao;
import com.yangtao.prototype.system.domain.User;
import com.yangtao.prototype.system.domain.UserRole;
import com.yangtao.prototype.system.user.service.UserService;
import com.yangtao.prototype.system.user.web.UserModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 杨涛
 * @version 1.0
 */
@Service
public class UserServiceImpl extends BaseService implements UserService {

	@Autowired
	private UserDao userDao;
	@Autowired
	private OrganizationDao organizationDao;
	@Autowired
	private RoleDao roleDao;

	/**
	 * 直接删除
	 * 同时要删除一个用户的所有相关信息
	 *
	 * @param model
	 * @return
	 */
	@Override
	public UserModel deleteUser(UserModel model) {
		String userId = model.getTargetUser().getId();
		userDao.delete(userId);
		model.addInfo("删除成功!");
		return model;
	}

	/**
	 * 根据用户对象相关数据信息查询符合数据条件分页数据集
	 * 要根据当前用户的Id查询当前用户所属机构下的所有用户
	 *
	 * @param model
	 * @return
	 */
	@Override
	public UserModel list(UserModel model) {
		//取得当前用户的组织机构的路径
		String currentUserOrganizeId = model.getOrganizationId();
		String currentUserOrganizePath = organizationDao.getOrganizePath(currentUserOrganizeId);
		userDao.findPageByCondition(model.getPage(), model.getCondition(), currentUserOrganizePath);
		return model;
	}

	/**
	 * 根据用户ID号加载用户单条数据信息
	 *
	 * @param model
	 * @return
	 */
	@Override
	public UserModel loadUser(UserModel model) {
		if (StringHelper.isNotEmpty(model.getTargetUser().getId())) {
			model.setTargetUser(userDao.load(model.getTargetUser().getId()));
		}
		return model;
	}

	/**
	 * 新增用户或修改用户时操作
	 * 新增: 新增时用户名不能重复，重复需要反馈用户名不能重复信息
	 * 修改: 修改时用户的中文名不能重复
	 *
	 * @param model
	 * @return
	 */
	@Override
	public UserModel updateUser(UserModel model) {
		//VO
		User userVo = model.getTargetUser();
		//主键
		String userId = userVo.getId();
		//PO
		User userPo = userDao.load(userId);
		//复制属性
		mapper.map(userVo, userPo);
		// 删除用户原有的角色
		userDao.deleteUserRoles(userId);
		//增加新的角色权限
		if (StringHelper.isNotEmpty(model.getUserRoles())) {
			String[] roles = model.getUserRoles().split(",");
			List<UserRole> urs = new ArrayList<UserRole>();
			for (String role : roles) {
				UserRole ur = new UserRole();
				ur.setRole(roleDao.load(role));
				ur.setUser(userPo);
				urs.add(ur);
			}
			userPo.setRoles(urs);
		}
		//用户所属组织机构
		String organizeId = userVo.getOrganizationId();
		if (StringUtils.isNotEmpty(organizeId)) {
			userPo.setOrganization(organizationDao.load(organizeId));
		}
		model.addInfo("save.success");
		return model;
	}

	/**
	 * 加载用户可分配的资源资源包括：
	 * 可分配的角色信息,
	 * 可分配的组织机构,
	 * 可分配的数据权限信息
	 *
	 * @param model
	 * @return
	 */
	@Override
	public UserModel loadAvailableResource(UserModel model) {
		String userId = model.getTargetUser().getId();
		String organizePath = organizationDao.getOrganizePath(model.getOrganizationId());
		//用户的已分配角色
		model.setDistributeRoles(userDao.findDistributeRoles(userId));
		//用户的未分配角色
		model.setUnDistributeRoles(userDao.findUnDistributeRoles(userId, organizePath));
		//用户可分配的组织机构
		model.setAvailableOrganize(organizationDao.getSubOrganization(model.getOrganizationId()));
		//TODO 增加可分配的数据权限
		return model;
	}

	/**
	 * 新增用户
	 * 新增时用户名不能重复
	 *
	 * @param model
	 * @return
	 */
	@Override
	public UserModel addUser(UserModel model) {
		//VO
		User userVo = model.getTargetUser();
		//PO
		User userPo = new User();
		//复制属性
		mapper.map(userVo, userPo);
		//增加新的角色权限
		if (StringHelper.isNotEmpty(model.getUserRoles())) {
			String[] roles = model.getUserRoles().split(",");
			List<UserRole> urs = new ArrayList<UserRole>();
			for (String role : roles) {
				UserRole ur = new UserRole();
				ur.setRole(roleDao.load(role));
				ur.setUser(userPo);
				urs.add(ur);
			}
			userPo.setRoles(urs);
		}
		//用户所属组织机构
		String organizeId = userVo.getOrganizationId();
		if (StringUtils.isNotEmpty(organizeId)) {
			userPo.setOrganization(organizationDao.load(organizeId));
		}
		userDao.save(userPo);
		model.addInfo("save.success");
		return model;
	}
}
